<?php

namespace App\ApiBackend\Admin;

use App\Http\Controllers\ApiBaseController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

/**
 *  管理员删除
 */
class Delete extends ApiBaseController
{

  /**
   * 参数检查
   */
  protected function check(): void
  {
    $ruleArr = [
      'adminId' => 'required',
    ];
    //验证
    $validator = Validator::make(request()->all(), $ruleArr);
    //验证失败
    $errorMsg = "";
    if ($validator->stopOnFirstFailure()->fails()) {
      $errorMsg = $validator->errors()->first();
    }
    if ($errorMsg) {
      $this->result["code"] = 1;
      $this->result["message"] = $errorMsg;
      return;
    }
    if (request('adminId') === 1) {
      $this->result["code"] = 9;
      $this->result["message"] = "管理员admin禁止删除";
    }
  }

  /**
   * 业务主体
   */
  protected function service(): void
  {
    //删除数据
    try {
      DB::table('admin')->where('adminId', request('adminId'))->delete();
      $this->result["message"] = "删除账户成功";
    } catch (\Exception $exception) {
      $this->result["code"] = 2;
      $this->result["message"] = "失败: " . $exception->getMessage();
    }
  }
}
